﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL;

namespace BLL
{
    public class KhachHangBLL
    {
        QLKSEntities db = new QLKSEntities();

        public object findCustomersByRoom(string maPhong)
        {
            QLKSEntities dbnew = new QLKSEntities();
            var query = from kh in dbnew.KhachHangs 
                        from lk in dbnew.LoaiKhaches 
                        where kh.MaLK == lk.MaLK && kh.MaPhong == maPhong
                        select new { kh.MaKH, kh.TenKH, kh.DiaChi, kh.GioiTinh, kh.SDT, kh.SoCMND, kh.MaPhong, kh.MaLK, lk.TenLK };
            return query.ToList();

            //return dbnew.KhachHangs.Where(kh => kh.MaPhong == maPhong).ToList<KhachHang>();
            //return db.KhachHangs.Where(kh => kh.MaPhong == maPhong).ToList<KhachHang>();
        }

        public string taoMaKH()
        {
            int layMaKHMax = 0;
            foreach (KhachHang kh in db.KhachHangs.ToList())
            {
                layMaKHMax = int.Parse(kh.MaKH.Substring(2));
            }
            int maMoi = layMaKHMax + 1;
            return "KH" + maMoi;
        }

        public bool themKH(string tenKH, string diaChi, bool gioiTinh, string sdt, string soCMND, string maPhong, string maLK)
        {
            KhachHang Kh = new KhachHang()
            {
                MaKH = taoMaKH(),
                TenKH = tenKH,
                DiaChi = diaChi,
                GioiTinh = gioiTinh,
                SDT = sdt,
                SoCMND = soCMND,
                MaPhong = maPhong,
                MaLK = maLK
            };

            try
            {
                db.KhachHangs.AddObject(Kh);
                db.SaveChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool xoaKH(string maKH)
        {
            KhachHang kh = db.KhachHangs.Where(kh1 => kh1.MaKH.Equals(maKH.Trim())).FirstOrDefault();
            try
            {
                db.KhachHangs.DeleteObject(kh);
                db.SaveChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool suaKH(string maKH, string tenKH, string diaChi, bool gioiTinh, string sdt, string soCMND, string maPhong, string maLK)
        {
            KhachHang kh = db.KhachHangs.Where(kh1 => kh1.MaKH == maKH).FirstOrDefault();
            try
            {
                kh.TenKH = tenKH;
                kh.DiaChi = diaChi;
                kh.GioiTinh = gioiTinh;
                kh.SDT = sdt;
                kh.SoCMND = soCMND;
                kh.MaPhong = maPhong;
                kh.MaLK = maLK;
                db.SaveChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
    }
}
